﻿<?php 
include_once 'public/DB_Oracle.php';
include_once 'public/page/page.class.php';
$DBsql = new DB_Oracle ();
//查询所以警员
if(isset($_GET['option'])){
	if($_GET['option']=='delete'){
		$sql="delete from dep_update t where t.id='".$_GET['str']."'";
		$DBsql->query($sql);
		exit();
		}
if($_GET['option']=="change"){
	$arr=explode(",",$_GET['str']);
for($i=0;$i<count($arr);++$i){
	$sql="select * from dep_update t where t.carid='".$arr[$i]."' and t.version='".$_GET['version']."' and t.state=0  and t.starttime in (select max(a.starttime) from dep_update a where a.carid='".$arr[$i]."')";
	$a=$DBsql->fetch_all_array($sql);
    if(count($a)==0){
$sql="insert into dep_update(id,version,state,starttime,carid)values(UP_SEQ.NEXTVAL,'".$_GET['version']."','0',sysdate,'".$arr[$i]."')";
	$DBsql->query($sql);
	}
	}
	echo "Y";
	}
	exit();
	}
//查询所有版本号
$sql="select t.version from update_file t";
$version=$DBsql->fetch_all_array($sql);
//查询所有的派出所
$sql="select dd.department_index, dd.department_name
		  from dep_department dd
		 order by dd.department_index";
$dedp=$DBsql->fetch_all_array($sql);



$sql="select * from dep_vehicle";
$rows=$DBsql->fetch_all_array($sql);
if(empty($_GET['departmentID'])){
$pageSize = 15;
$total=count($rows)== 0 ? 1 : count($rows);
//第一个0显示数字  第二个1显示概况 第三个显示下拉 第四个显示数字数量
pageft($total, $pageSize, 1, 1, 1, 5);
$displaypg+=$firstcount;
$sql="select *
	  from (select rownum r,b.vehicle_index,b.license_plate,TO_CHAR(b1.starttime, 'YYYY-MM-DD HH24:MI:SS') starttime,b1.version,b1.state,b1.id from dep_vehicle b left join (select t1.* from dep_update t1,(select t.carid,max(t.starttime) stime from dep_update t group by t.carid) t2 
where t1.carid=t2.carid and t1.starttime=t2.stime) b1 on  b.vehicle_index=b1.carid) c
	 where c.r > $firstcount
	   and c.r <= $displaypg";
$rows=$DBsql->fetch_all_array($sql);
}
else
{
	$depID=$_GET['departmentID'];
			$sql="select dp.vehicle_index from dep_vehicle dp where dp.department_index=$depID";
	$rows=$DBsql->fetch_all_array($sql);
$pageSize = 15;
$total=count($rows)== 0 ? 1 : count($rows);
//第一个0显示数字  第二个1显示概况 第三个显示下拉 第四个显示数字数量
pageft($total, $pageSize, 1, 1, 1, 5);
$displaypg+=$firstcount;
$sql="select *
	  from (select rownum r,b.vehicle_index,b.license_plate,TO_CHAR(b1.starttime, 'YYYY-MM-DD HH24:MI:SS') starttime,b1.version,b1.state,b1.id from (select * from dep_vehicle where department_index=$depID) b left join (select t1.* from dep_update t1,(select t.carid,max(t.starttime) stime from dep_update t group by t.carid) t2 
where t1.carid=t2.carid and t1.starttime=t2.stime) b1 on  b.vehicle_index=b1.carid ) c
	 where c.r > $firstcount
	   and c.r <= $displaypg";
$rows=$DBsql->fetch_all_array($sql);
	}
?>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/validform.css" rel="stylesheet" type="text/css" />
<link href="public/page/css/css.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript" src="js/jquery.js"></script>
<script language="javascript" type="text/javascript" src="js/public.js"></script> 
<script language="javascript">
function department(str){
	var url="dep_update.php?"+encodeURI("dep="+str);
	window.location=url;  
	}
function CheckAll(obj){var chebox=document.getElementsByName('checkbox_id');if(obj.checked==true){
for(var i=0;i<chebox.length;++i){chebox.item(i).checked=true;}}else{for(var i=0;i<chebox.length;++i){chebox.item(i).checked=false;}}
}


function UP(){
	var obj=document.getElementById('select').value;
	if(obj==0){
		alert("没有选择版本号");
		return 0;
		}
var chebox=document.getElementsByName('checkbox_id');
var n=0;
var str="";
for(var i=0;i<chebox.length;++i){
	if(chebox.item(i).checked==true){
		n++;
		str+=chebox.item(i).value+",";
		}}
		if(n==0){
			alert("没有选择要更新内容的车辆");
			return 0;
			}
			str=str.substr(0,str.length-1);
    var str_arr=[str,obj];
	
			$.ajax({
			type:"GET",
			url:"dep_update.php",
			cache:false,
			data:encodeURI("option=change&str="+str_arr[0]+"&version="+str_arr[1]),
			success:function(data){
					alert('更新任务下发成功'); 
	window.location="dep_update.php";
			}
		});
		}
document.ondblclick = function(){
	POPdiv('selectLocation');
};
function groupSelect(){
	var policeID=$("#selectID").val();
	$('#departmentID').val(policeID);
	$('#Location').submit();
}

function v_d(id){
	$.ajax({
			type:"GET",
			url:"dep_update.php",
			cache:false,
			data:encodeURI("option=delete&str="+id),
			success:function(data){
					alert('停止成功'); 
	window.location="dep_update.php";
			}
		});
	}
</script>
 
<!--管理列表开始-->
<div class="manageTab" style="margin-top:0px;">
  <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" class="usrTab">
    <tr>
      <td width="8" height="25"><img src="images/user_01.jpg" height="25" width="8" /></td>
      <td style="background:#3980b2;" valign="middle" height="16" ><div class="use_righgDiv"><span>
          <input type="checkbox" id="checkall" name="" value="" onclick="CheckAll(this)" />
          全选</span>
          <span>
          <select id="select"><option value="0">前选择版本号</option><?PHP for($i=0;$i<count($version);++$i){
			  echo "<option value='".$version[$i]['VERSION']."'>".$version[$i]['VERSION']."</option>";
			  }
			  ?></select></span><span><input type="button" onclick="UP()" value="更新" /></span></div>
        <div class="use_leftDiv">
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td  width="22"><img src="images/311.gif" height="16" width="16" /></td>
              <td align="left"><span class="tdTit">版本更改</span></td>
            </tr>
          </table>
        </div></td>
      <td width="8" height="25"><img src="images/user_02.gif" height="25" width="8" /></td>
    </tr>
    <tr>
      <td style="background:#3980b2;" >&nbsp;</td>
      <td><table width="100%" border="0" cellspacing="1" cellpadding="0" align="center" class="usrList" id="List">
          <tr>
            <td  class="td90b2">选择</td>
            <td  class="td90b2">车牌号</td>
             <td  class="td90b2">版本号</td>
            <td  class="td90b2">提交更新时间</td>
             <td  class="td90b2">是否更新</td>
             <td  class="td90b2">停止更新</td>
          </tr>
          <?php if(count($rows)!=0){
          		foreach ($rows as $ros){
          	?>
	          <tr>
	            <td class="tdfff"><input type='checkbox' name="checkbox_id" class="STYLE2" value="<?php echo $ros['VEHICLE_INDEX'];?>" /></td>
	            <td class="tdfff"><?php echo $ros['LICENSE_PLATE'];?></td>
	            <td class="tdfff"><?php if($ros['VERSION']==''){echo "没有版本号信息";}else{echo $ros['VERSION'];}?></td>
	            <td class="tdfff"><?php if($ros['STARTTIME']==''){echo "没有时间信息";}else{ echo $ros['STARTTIME'];}?></td>
	            <td class="tdfff"><?php if($ros['STATE']==0){echo "没有更新";}else{ echo "更新";}?></td>
                <td class="tdfff"><input type="button" <?php if($ros['ID']==''||$ros['STATE']==1){echo "disabled=\"disabled\"";}?> value="停止" onclick="v_d('<?php echo $ros['ID'];?>')"/></td>
	          </tr>
          <?php }}else{echo '暂无数据！';}?>
          <tr>
            <td colspan="10" class="tdfff">&nbsp;</td>
          </tr>
        </table></td>
      <td style="background:#3980b2;">&nbsp;</td>
    </tr>
    <tr>
      <td height="24" width="8"><img src="images/user_03.gif" height="24" width="8" /></td>
      <td style="background:#3980b2;"><div class="green-black"><?php echo $pagenav;?>
        </div></td>
      <td height="24" width="8"><img src="images/user_04.gif" height="24" width="8" /></td>
    </tr>
  </table>
</div>
<!--管理列表结束-->
<!--弹出层选择派出所开始-->
<div id="selectLocation" class="POPdiv">
	 <div class="POPdiv_top"><a href="javascript:void(null)" onclick="ClosePOPdiv('selectLocation')"><img src="images/ico_close.jpg" alt="关闭" /></a>分组查看</div>
        <div class="POPdiv_content">
        	<form id="Location" action="dep_update.php" method="get">
	        	 <table border="0" cellpadding="5" cellspacing="0">
				    <tr>
				      <td width="80"><label for="selectID">选择派出所</label></td>
				      <td>
					  	<select id="selectID" name="selectID" onchange="groupSelect()">
							<option value="">全部</option>
							 <?php 
					          	foreach ($dedp as $row){
					         ?>
					         	<option <?php if(isset($_GET['departmentID'])){if($_GET['departmentID']==$row['DEPARTMENT_INDEX']){echo 'selected=selected';}}?> value="<?php echo $row['DEPARTMENT_INDEX'];?>"><?php echo $row['DEPARTMENT_NAME'];?></option>	
					         <?php
					          	}
					         ?>
						</select>
					  </td>
				      <td><input type="hidden" id="departmentID" name="departmentID" value=""></td>
				    </tr>
				 </table>
			 </form>
		</div>
</div>
<!--弹出层选择派出所结束-->